package com.ymatou.iapi.prod.testcase;

import java.util.Date;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.prod.parameter.GetProductSnapshotInfoBean;
import com.ymatou.iapi.prod.service.GetProductSnapshotInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerAppProdService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductCoreInfoWapper;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductVersionInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

public class Ts_GetProductSnapshotInfo {
	
	private static GetProductSnapshotInfoBean getProductSnapshotInfoBean;
	private static GetProductSnapshotInfoCall getProductSnapshotInfoCall;
	private static BuyerAppProdService buyerAppProdService = new BuyerAppProdService();
	
	private static int sellerId = Integer.parseInt(EnvSetup.getData("zhangyi_seller002_id"));
	private static String sellerName = EnvSetup.getData("zhangyi_seller002_name");
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品商品快照详情");
	}

	@Before
	public void caseUp() throws Exception {
		getProductSnapshotInfoBean = new GetProductSnapshotInfoBean();
		getProductSnapshotInfoCall = new GetProductSnapshotInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductSnapshotInfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_001() {
		 Logger.start(true,"不修改商品，商品快照详情接口数据与mongodb验证");
		 try {
			 String productId = SellerProductCallService.addNewProductOnSale(sellerId, 
					 sellerName, null).getJSONObject("Data").getString("ProductID");

			 getProductSnapshotInfoBean.setProductId(productId);
			 getProductSnapshotInfoBean.setVersion("1.001");
			 
			 getProductSnapshotInfoCall.setData(getProductSnapshotInfoBean);
			 getProductSnapshotInfoCall.callService();
			 
			 this.verifyNormalData(productId, getProductSnapshotInfoCall, "1.001");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_002() {
		 Logger.start(true,"修改商品，修改前商品快照详情接口数据与mongodb验证");
		 try {
			 String productId = buyerAppProdService.modifyProduct(sellerName, sellerId);
			 //String productId = "5ce247f7-cae9-4685-99d8-2677672925b1";
			 getProductSnapshotInfoBean.setProductId(productId);
			 getProductSnapshotInfoBean.setVersion("1.001");
			 
			 getProductSnapshotInfoCall.setData(getProductSnapshotInfoBean);
			 getProductSnapshotInfoCall.callService();
			 
			 this.verifyNormalData(productId, getProductSnapshotInfoCall, "1.001");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_003() {
		 Logger.start(true,"修改商品，最新商品快照详情接口数据与mongodb验证");
		 try {
			 String productId = buyerAppProdService.modifyProduct(sellerName, sellerId);

			 getProductSnapshotInfoBean.setProductId(productId);
			 getProductSnapshotInfoBean.setVersion("2.001");
			 
			 getProductSnapshotInfoCall.setData(getProductSnapshotInfoBean);
			 getProductSnapshotInfoCall.callService();
			 
			 this.verifyNormalData(productId, getProductSnapshotInfoCall, "2.001");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Ts_GetProductInfo_004() {
		 Logger.start(true,"修改商品，请求不存在的商品快照");
		 try {
			 String productId = buyerAppProdService.modifyProduct(sellerName, sellerId);

			 getProductSnapshotInfoBean.setProductId(productId);
			 getProductSnapshotInfoBean.setVersion("3.001");
			 
			 getProductSnapshotInfoCall.setData(getProductSnapshotInfoBean);
			 getProductSnapshotInfoCall.callService();
			 
			 Logger.verifyEquals("null", new JSONObject(getProductSnapshotInfoCall.getReturnData()).getString("result"), 
					 "验证不存在的商品快照result数据");
			 
		 } catch (Exception e) {
			 Logger.fail(e);
		 }
	 }
	
	public void verifyNormalData(String productId, GetProductSnapshotInfoCall getProductSnapshotInfoCall, String version) throws Exception {
		 int tariffType = 0;
		 
		 ProductDetailModelWapper productDetailModelWapper = new ProductDetailModelWapper();
		 BuyerAppProdService prodService = new BuyerAppProdService();
		 Map<String,Object> productDetailModelMap = productDetailModelWapper.getProductId(productId);
		 JSONArray picsArray = new JSONArray(productDetailModelMap.get("PicList").toString());
		 JSONArray skus = new JSONArray(getProductSnapshotInfoCall.getResult().get("skus").toString());
		 String catalogId = new JSONObject(skus.get(0).toString()).getString("id");
		 
		 String latestVersion = productDetailModelMap.get("Version").toString();
		 
		 if (!this.getShowSnapshot(version, latestVersion)) {
			 
			 ProductVersionInfoWapper productVersionInfoWapper = new ProductVersionInfoWapper();
			 String pKey = productVersionInfoWapper.getVersionInfoByProductIdAndVer(productDetailModelMap.
					 get("ProductId").toString(), this.getIntegerVersion(version)).get("pkey").toString();
			 
			 ProductCoreInfoWapper productCoreInfoWapper = new ProductCoreInfoWapper();
			 Map<String, Object> productCoreInfoMap = productCoreInfoWapper.getProductCoreInfoByPkey(pKey);
			 
			 Logger.verifyEquals(productCoreInfoMap.get("title"), 
					 getProductSnapshotInfoCall.getResult().get("title"), "验证修改前的title是否与mongodb一致");
			 Logger.verifyEquals(productCoreInfoMap.get("intro"), 
					 getProductSnapshotInfoCall.getResult().get("intro"), "验证修改前的title是否与mongodb一致");
			 
			 return;
		 }

		 Logger.verifyEquals(productDetailModelMap.get("ProductId"), 
				 getProductSnapshotInfoCall.getResult().get("id"), "验证id是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("Title"), 
				 getProductSnapshotInfoCall.getResult().get("title"), "验证title是否与mongodb一致");
		 
		 for (int i = 0; i < picsArray.length(); i++) {
			 Logger.verifyEquals(picsArray.get(i).toString(), 
					 getProductSnapshotInfoCall.getResult().getJSONArray("picList").getJSONObject(i).getString("original"), "验证pics是否与mongodb一致");
		 }
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap.get("ValidStart")).toString()), 
				 this.changeUNIXTime(getProductSnapshotInfoCall.getResult().getLong("putawayTime")), "验证putawayTime是否与mongodb一致");
		 Logger.verifyEquals(this.getCST2TimeStamp(((Date) productDetailModelMap.get("ValidEnd")).toString()), 
				 this.changeUNIXTime(getProductSnapshotInfoCall.getResult().getLong("expireTime")), "验证expireTime是否与mongodb一致");
		 
		 if (productDetailModelMap.get("TariffType").toString().equalsIgnoreCase("0") 
				 && productDetailModelMap.get("Freight").toString().equalsIgnoreCase("0.00"))
			 tariffType = 3;
		 
		 Logger.verifyEquals(tariffType, getProductSnapshotInfoCall.getResult().get("tariffType"), 
				 "验证tariffType是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("DeliveryMethod"), 
				 getProductSnapshotInfoCall.getResult().get("deliveryType"), "验证deliveryType是否与mongodb一致");
		 
		 Logger.verifyEquals(prodService.getCatalogPriceFromMongo(0, productId, catalogId).getInt("Price"), 
				 new JSONObject(skus.get(0).toString()).getInt("original"), "验证规格的原价是否与mongodb一致");
		 Logger.verifyEquals(productDetailModelMap.get("NoReasonReturn"), 
				 getProductSnapshotInfoCall.getResult().getBoolean("isNoReasonReturn"), "验证isNoReasonReturn是否与mongodb一致");
	}
	
	//将unix时间戳毫秒数置为000
	public long changeUNIXTime(long timeStamp) {
		return Long.parseLong(String.valueOf(timeStamp).substring(0, 10).concat("000"));
	}
	
	public long getCST2TimeStamp(String dateStr) {
		return YMTDateUtil.CST2TimeStamp(dateStr);
	}
	
	/**
	 * 将1.001变成1001
	 * 
	 * @param version(传入的商品版本)
	 * @return Integer 
	 * @throws Exception
	 */
	public int getIntegerVersion(String version) {
		String temp = version.split("\\.")[0] + version.split("\\.")[1];
		
		return Integer.valueOf(temp);
	}
	
	/**
	 * 判断商品是否需要跳转快照
	 * 
	 * @param version(传入的商品版本)，productVersion(最新的商品版本)
	 * @return Boolean
	 * @throws Exception
	 */
	public Boolean getShowSnapshot(String version, String productVersion) throws Exception {
		if (version.equalsIgnoreCase("0"))
			return false;
		else {
			int bigVersion = Integer.valueOf(version.split("\\.")[0]);
			int productBigVersion = Integer.valueOf(productVersion.split("\\.")[0]) / 1000;
			
			if (bigVersion == productBigVersion)
				return true;
			else
				return false;
		}
	}
}
